package com.jmu.edu.IMApplication.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.google.android.material.button.MaterialButton;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.utils.ViewUtils;

import java.io.File;

import cn.jpush.im.android.api.callback.DownloadCompletionCallback;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.model.Message;

public class ImageDetailFragment extends Fragment {
    private PhotoView imageView;
    private Message imageMessage;
    private MaterialButton backButton;

    public void setImageMessage(Message imageMessage) {
        this.imageMessage = imageMessage;
    }

    public ImageDetailFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_image_detail, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        imageView = getView().findViewById(R.id.fragment_image_detail_image);
        backButton = getView().findViewById(R.id.fragment_image_detail_back);
        initListener();
        loadData();
    }

    private void initListener() {
        imageView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView imageView, float v, float v1) {
                backButton.callOnClick();
            }
        });
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewUtils.hideFragment(ImageDetailFragment.this, R.anim.from_right, R.anim.out_right);
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {

        } else {
            loadData();
        }
    }

    private void loadData() {
        ViewUtils.addBackListenerForHide(getView(), backButton);
        ImageContent imageContent = (ImageContent) imageMessage.getContent();
        imageContent.downloadOriginImage(imageMessage, new DownloadCompletionCallback() {
            @Override
            public void onComplete(int i, String s, File file) {
                if (i == 0) {
                    Glide.with(imageView).load(file).into(imageView);
                } else {
                    Glide.with(imageView).load(R.drawable.smc).into(imageView);
                }

            }
        });

    }
}